﻿using System;
using NonLeagueExplorer.Data.Interfaces;


namespace NonLeagueExplorer.Data
{
    /// <summary>
    /// Concrete Fixture class, holds details for a particular fixture
    /// and methods related to the fixture to be able to access data and 
    /// allow manipulation of the data.
    /// </summary>
    public class Fixture : IFixture, IComparable
    {
        protected string HomeTeam, AwayTeam;
        protected DateTime FixtureDateTime;
        protected Score m_Score = null;
        protected bool m_Postponed, m_Abandonded;

        public Fixture(DateTime p_Date, string p_Home, string p_Away)
        {
            FixtureDateTime = p_Date;
            HomeTeam = p_Home;
            AwayTeam = p_Away;

            m_Postponed = false;
            m_Abandonded = false;
        }

        public override bool Equals(object obj)
        {
            bool equal = false;
            Fixture tempFix = (Fixture)obj;

            equal = tempFix.HomeTeam == this.HomeTeam;
            equal &= tempFix.AwayTeam == this.AwayTeam;
            equal &= tempFix.FixtureDateTime == this.FixtureDateTime;
            equal &= tempFix.m_Postponed == this.m_Postponed;
            equal &= tempFix.m_Abandonded == this.m_Abandonded;
            equal &= tempFix.m_Score == this.m_Score;

            return equal;
        }

        #region public properties

        public string Home
        {
            get { return HomeTeam; }
        }

        public string Away
        {
            get { return AwayTeam; }
        }

        public DateTime Date
        {
            get { return FixtureDateTime; }
        }

        public Score FinalScore
        {
            get { return m_Score; }
        }

        public bool Postponed
        {
            get { return m_Postponed; }
        }

        public bool Abandoned
        {
            get { return m_Abandonded; }
        }

        #endregion

        #region IFixture Members

        public void SetHomeTeam(string p_Name)
        {
            HomeTeam = p_Name;
        }

        public void SetAwayTeam(string p_Name)
        {
            AwayTeam = p_Name;
        }

        public void SetFinalScore(Score p_FinalScore)
        {
            m_Score = p_FinalScore;
        }

        public void SetDate(DateTime p_Date)
        {
            FixtureDateTime = p_Date;
        }

        public void Postpone()
        {
            m_Postponed = true;
        }

        public void Abandon()
        {
            m_Abandonded = true;
        }

        public bool IsPostponed()
        {
            return m_Postponed;
        }

        public bool IsAbandoned()
        {
            return m_Abandonded;
        }

        public Score GetScore()
        {
            return m_Score;
        }

        public string GetHomeTeam()
        {
            return HomeTeam;
        }

        public string GetAwayTeam()
        {
            return AwayTeam;
        }

        public DateTime GetDate()
        {
            return FixtureDateTime;
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            int result = 0;

            if (obj is Fixture)
            {
                Fixture fix = (Fixture)obj;
                if (this.FixtureDateTime < fix.GetDate())
                {
                    result = -1;
                }
                else if (this.FixtureDateTime > fix.GetDate())
                {
                    result = 1;
                }
                else
                {
                    result = HomeTeam.CompareTo(fix.GetHomeTeam());
                }
            }
            else
            {
                throw new ArgumentException("Incorrect object type comparison.");
            }

            return result;
        }

        #endregion
    }
}
